#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：django32_celery 
@File    ：uploadoss2.py
@IDE     ：PyCharm 
@Author  ：Toby
@Date    ：2024/5/22 10:55 
'''

import os
import oss2
import json
import time
import schedule

# pip install schedule -i https://mirrors.aliyun.com/pypi/simple/
# pip install oss2


# 阿里云 OSS 访问信息
access_key_id = 'xxxxxxxxx'
access_key_secret = 'xxxxxxxxxxxxx'
endpoint = 'xxxxxxxxxxxxx'
bucket_name = 'bucketName'    #创建存储空间（Bucket）

# 本地目录路径
local_directory = '/home/test'

# 记录上次上传的状态，可以使用本地文件、数据库或其他方式进行记录
# 这里使用一个简单的本地文件来记录状态
state_file = './upload_state.txt'

# 创建 OSS 客户端
auth = oss2.Auth(access_key_id, access_key_secret)
bucket = oss2.Bucket(auth, endpoint, bucket_name)

# 获取上次上传的状态，如果没有则初始化为空字典
# upload_state = {}
if os.path.exists(state_file):
    sz = os.path.getsize(state_file)
    if not sz:
        upload_state = {}
    else:
        with open(state_file, 'r') as f:
            upload_state = json.load(f)
else:
    upload_state = {}


# 递归上传本地目录及其子目录下的文件
# local_directory  本地目录
# bucket oss bucket名称
# oss_directory  oss 下的目录路径，注意，多级目录时前后不需要加‘/’
def upload_directory_to_oss(local_directory, bucket, oss_directory='logs'):
    for root, dirs, files in os.walk(local_directory):
        for file in files:
            local_file_path = os.path.join(root, file)
            oss_object_key = os.path.join(oss_directory, os.path.relpath(local_file_path, local_directory))

            # 检查文件是否需要上传
            if should_upload(local_file_path, oss_object_key):
                # 上传文件到 OSS
                bucket.put_object_from_file(oss_object_key, local_file_path)
                print(f'Uploaded {local_file_path} to {oss_object_key}')

                # 记录上传状态
                upload_state[oss_object_key] = os.path.getmtime(local_file_path)

                # 成功上传后，删除本地源文件
                # os.remove(local_file_path)
                # print(f'Deleted {local_file_path}')


# 判断文件是否需要上传
def should_upload(local_file_path, oss_object_key):
    if oss_object_key not in upload_state:
        # 文件未上传过，需要上传
        return True
    else:
        # 文件已上传过，检查本地文件的修改时间是否大于上传时间
        local_mtime = os.path.getmtime(local_file_path)
        return local_mtime > upload_state[oss_object_key]



# 保存上传状态到本地文件
with open(state_file, 'w') as f:
    json.dump(upload_state, f)

def job():
    # 开始上传
    upload_directory_to_oss(local_directory, bucket)



# 设定时间：每周星期一早上7点钟发送邮件
schedule.every().day.at("00:30").do(job)
while True:
    schedule.run_pending()
    time.sleep(3)